package com.longdaji.decoration.ui.main.cart;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.CartBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class CartPresenter extends RxPresenter<CartContract.View> implements CartContract.Presenter {
    private final static String TAG = CartPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public CartPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }


    @Override
    public void getCartList(String userId) {
        addSubscribe(mDataManager.getCartList(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<CartBean>handleResult())
                .subscribeWith(new CommonSubscriber<CartBean>() {


                    @Override
                    public void onSuccess(CartBean cartBean) {

                        if (mView != null) {
                            mView.showCartList(cartBean.getCart());
                        }

                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showCartList(new ArrayList<CartBean.Cart>());
                        }
                    }
                }));
    }

    @Override
    public void deleteChartGoods(String userId, final List<CartBean.Cart> cartGoodsList) {
        String[] idList = new String[cartGoodsList.size()];
        for (int i = 0; i < cartGoodsList.size(); i++) {
            idList[i] = cartGoodsList.get(i).getCartGoodsRecordId();
        }
        Log.d(TAG, "deleteChartGoods idList=" + idList.toString());
        addSubscribe(mDataManager.deleteFromChart(userId, idList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (mView != null) {
                            Log.d(TAG, "deleteChartGoods" + httpResponse);
                            if ("0".equals(httpResponse.getStatusCode())) {
                                mView.showDeleteCartSuccess(cartGoodsList);
                            } else {
                                mView.showDeleteCartFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void collectChartGoods(String userId, List<CartBean.Cart> cartGoodsList) {
        List<String> idList = new ArrayList<>();
        for (int i = 0; i < cartGoodsList.size(); i++) {
            idList.add(cartGoodsList.get(i).getGoodsId());
        }
        // Log.d(TAG, "collectChartGoods idList=" + idList.toString());
        addSubscribe(mDataManager.collectGoodsList(userId, idList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (mView != null) {
                            Log.d(TAG, "collectChartGoods" + httpResponse);
                            if ("0".equals(httpResponse.getStatusCode())) {
                                mView.showCollectSuccess();
                            } else {
                                mView.showCollectFail();
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void modifyCartItemNum(Map<String, String> options, final int position, final long num) {
        addSubscribe(mDataManager.modifyCartItemNum(options)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (mView != null) {
                            Log.d(TAG, "modifyCartItemNum" + httpResponse);
                            if ("0".equals(httpResponse.getStatusCode())) {
                                mView.changeCartItemNum(position, num);
                            } else {
                                mView.changeCartItemNumFail(httpResponse.getMessage());
                            }
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }


}
